$(function() {
	$("#form").validate({
		rules : {
			"contract.contracttype" : {required : true},
			"contract.startdate" : {required : true},
			"contract.enddate" : {required : true},
			"contract.loangranttype" : {required : true},
			"contract.contractdate" : {required : true},
			"loangranttype" : {required : true}
		},
			messages : {
			"contract.contracttype" : {required : '请选择合同类型!'},
			"contract.startdate" : {required : '请选择合同开始日期!'},
			"contract.enddate" : {required : '请选择合同终止日期!'},
			"contract.loangranttype" : {required : '请选择贷款发放次数!'},
			"contract.contractdate" : {required : '请选择合同签订日期!'},
			"loangranttype" : {required : '请选择贷款发放次数!'}
		}
	});
	
		show_percent();
		$("#granttime").keyup(function(){
			$(".thisnotr").remove();
			$granttime=$("#granttime").val();
			for($i=1;$i<=$granttime;$i++){
				$trHTML="<tr class='thisnotr'><td align='right'><span class='need'>*</span>发放日期</td><td><input class='datep' type='text' name='grantdate' value='' /></td><td align='right'><span class='need'>*</span>到期日期</td><td><input class='dateq' type='text' name='refunddate' value='' /></td><td align='right'><span class='need'>*</span>发放金额</td><td><input type='text' name='grantmoney' value='' /></td></tr>";
				$("#thisisatable").append($trHTML);//在table最后面添加一行
			}			
			$granttime=$("#granttime").val();
			if($granttime>20){
				alert("发放次数不能超过20次");
				once_click();
				return ;
			}
			var a = $granttime.match(/\d*\.?\d{0,2}/,'$1');
			if($granttime==""){
				alert("请填写发放次数");
				once_click();
				return ;
			}
			if(a!=$granttime){
				alert("发放次数请输入数字");
				once_click();
				return ;
			}
			var b=parseInt($granttime);
			if(b!=$granttime){
				alert("发放次数请输入整数");				
				once_click();
				return ;
			}
			$(".datep").datetimepicker({
				dateFormat: "yy-mm-dd",
				showTimepicker: false,changeMonth: true,changeYear: true});
			$(".dateq").datetimepicker({
				dateFormat: "yy-mm-dd",
				showTimepicker: false,changeMonth: true,changeYear: true});
		});
		
		$("#granttime").attr("readonly","readonly");
		$("#granttime").css("background-color","#999");
		$("#once").click(function(){
			once_click();
		});	
		$("#loangrant").click(function(){
			$("#granttime").removeAttr("readonly");
			$("#granttime").css("background-color","#fff");

		});
		
})

function once_click(){
	$("#loangrant").removeAttr("checked");
	$("#granttime").attr("readonly","readonly");
	$("#granttime").css("background-color","#999");
	$("#granttime").val("");
	$(".thisnotr").remove();
}

function show_percent(){
	var  toUpper = $("#toUpper");
	toUpper.css("color","#000");

	var num = $("#srcLower").val();
	var a = num.match(/\d*\.?\d{0,2}/,'$1');
	if(a!=$.trim(num)){
		toUpper.val("输入有误!!")
		toUpper.css("color","red");
		return;
	}
	var upperValue = Money.toUpper($.trim(num));
	toUpper.val(upperValue);
}